﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace BlackBBS.Models.Data
{
    [Serializable]
    public class ConfigSettings
    {
        public User SysOp { get; set; }

        public string BBSName { get; set; }
        public string BBSRootPath { get; set; }
        public string ANSPath { get; set; }
        public string RepositoryPath { get; set; }
        public string DBHostname { get; set; }
        public string DoorPath { get; set; }
        public string NetFossilPath { get; set; }
        public string ScriptVariableIndicator { get; set; }
        public string EthernetPhysicalAddressToUse { get; set; }
        public int Port { get; set; }
        public int ClientPortBase { get; set; }
        public string Version { get; set; }

        public static ConfigSettings Setup()
        {
            var results = new ConfigSettings();

            // Set main directory
            results.BBSRootPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
#if DEBUG
            results.BBSRootPath = @"C:\bbs\";
#endif
            // Veriables read from config
            results.BBSName = ConfigurationManager.AppSettings["BBSName"];
            results.ANSPath = Path.Combine(results.BBSRootPath, ConfigurationManager.AppSettings["ANSPath"]);
            results.DoorPath = Path.Combine(results.BBSRootPath, ConfigurationManager.AppSettings["Doors"]);
            results.RepositoryPath = Path.Combine(results.BBSRootPath, ConfigurationManager.AppSettings["RepositoryPath"]);
            results.NetFossilPath = Path.Combine(results.BBSRootPath, "netfoss");
            results.ScriptVariableIndicator = ConfigurationManager.AppSettings["ScriptVariableIndicator"];
            results.EthernetPhysicalAddressToUse = ConfigurationManager.AppSettings["EthernetPhysicalAddressToUse"];
            results.Port = Convert.ToInt32(ConfigurationManager.AppSettings["PortToUse"]);
            results.DBHostname = ConfigurationManager.AppSettings["DBHostname"];
            results.ClientPortBase = 23300;
            results.Version = "1.0.0";
            return results;
        }
    }

    public static class ConfigSettingsExt
    {
        public static string MenuFullPath(this ConfigSettings settings, string menu)
        {
            return System.IO.Path.Combine(settings.ANSPath, menu);
        }

        public static string CurrentNodeExtProgramsFullPath(this ConfigSettings settings, int node)
        {
            return Path.Combine(settings.BBSRootPath, string.Format("node{0}", node.ToString("00")));
        }
    }
}
